home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import contextlib
- import sys
- from thread import get_ident
- from peak.util.decorators import cache_source, classy
- __all__ = [
- 'Service',
- 'replaces',
- 'setting',
- 'InputConflict',
- 'DynamicRuleError',
- 'State',
- 'Action',
- 'resource',
- 'registry',
- 'new',
- 'empty',
- 'lookup',
- 'manager',
- 'reraise',
- 'with_',
- 'call_with',
- 'ScopeError',
- 'resource_registry']
- _in_place = '__iadd__ __isub__ __imul__ __idiv__ __itruediv__ __ifloordiv__\n__imod__ __ipow__ __ilshift__ __irshift__ __iand__ __ixor__ __ior__'.split()
- _ignore = dict.fromkeys('\n __name__ __module__ __return__ __slots__ get __init__ __metaclass__ __doc__\n __call__ __new__'.split() + _in_place).__contains__
-
- def _no_in_place(self, *args):
- raise TypeError('In-place operators (other than <<=) cannot be performed on a service class')
-
-
- def _ilshift(cls, factory):
- State[cls] = factory
- return cls
-
-
- def _mod(cls, expr):
- return 'lambda: ' + expr
-
- _std_attrs = []([ (k, _no_in_place) for k in _in_place ], __ilshift__ = _ilshift, __mod__ = _mod)
-
- def redirect_attribute(cls, name, payload):
- setattr(type(cls), name, (property,)(((lambda s: getattr(s.get(), name)),), ((lambda s, v: setattr(s.get(), name, v)),), (lambda s: delattr(s.get(), name))))
-
-
- class _ClassDelegate(classy):
- __slots__ = ()
- get = None
-
- def __class_init__(cls, name, bases, cdict, supr):
- meta = type(cls)
- if getattr(meta, '__for_class__', None) is not cls:
- cls.__class__ = meta = type(meta)(cls.__name__ + 'Class', (meta,), dict(_std_attrs, __module__ = cls.__module__, __for_class__ = cls))
-
- supr()(cls, name, bases, cdict, supr)
- if 'get' not in cdict:
- cls.get = staticmethod(classmethod(lookup).__get__(None, cls))
-
- for k, v in cdict.items():
- if not isinstance(k, basestring):
- continue
-
- if not isinstance(v, (classmethod, staticmethod)) and not _ignore(k):
- redirect_attribute(cls, k, v)
- continue
-
-
- __class_init__ = classmethod(__class_init__)
-
-
- class State(_ClassDelegate):
-
- def __new__(cls, *rules, **attrs):
- if not attrs or object.__new__(cls):
- pass
- return empty()
-
-
- def __init__(self, **attrs):
- self.__dict__.update(attrs)
-
-
- def __getitem__(self, key):
- return self.getRule(key)
-
-
- def __setitem__(self, key, rule):
- return self.setRule(key, rule)
-
-
- def swap(self):
- raise NotImplementedError("Can't switch to the root state")
-
-
- def child(self, *rules):
- raise NotImplementedError
-
-
- def __enter__(self):
- raise NotImplementedError("Can't enter the root state")
-
-
- def __exit__(self, typ, val, tb):
- raise NotImplementedError("Can't exit the root state")
-
-
- def on_exit(self, callback):
- raise NotImplementedError
-
-
- def get(key = None):
- raise NotImplementedError
-
- get = staticmethod(get)
- parent = None
-
-
- class InputConflict(Exception):
- pass
-
-
- class DynamicRuleError(Exception):
- pass
-
-
- class ScopeError(Exception):
- pass
-
- _exc_info = { }
- nones = (None, None, None)
-
- def _swap_exc_info(data):
- this_thread = get_ident()
- old = _exc_info.get(this_thread, nones)
- _exc_info[this_thread] = data
- return old
-
-
- def new():
- return State.child()
-
-
- def _let_there_be_state():
- states = { }
-
- def _swap(what):
- this_thread = get_ident()
- old = states.setdefault(this_thread, what)
- states[this_thread] = what
- return old
-
-
- def lookup(key):
-
- try:
- (state, getRule, lookup, child) = states[get_ident()]
- except KeyError:
- empty().swap()
- (state, getRule, lookup, child) = states[get_ident()]
-
- return lookup(key)
-
-
- def get(key = ((None, (None,)), None)):
-
- try:
- (state, getRule, lookup, child) = states[get_ident()]
- except KeyError:
- empty().swap()
- (state, getRule, lookup, child) = states[get_ident()]
-
- if key is None:
- return state
-
- return getRule(key)
-
-
- def disallow(key):
- raise DynamicRuleError('default rule or exit function tried to read dynamic state', key)
-
-
- def empty():
- state = new_state(root_getrule)
- state.parent = root
- return state
-
-
- def new_state(inherit = None, inheritedDistances = None, propagate = (None, (None, None, None), None, None, None)):
- buffer = { }
- rules = { }
- values = { }
- distances = { }
- computing = { }
- get_stack = computing.get
- set_stack = computing.setdefault
-
- def getRule(key):
-
- try:
- rule = rules[key]
- except KeyError:
-
- try:
- rule = buffer[key]
- except KeyError:
- rule = buffer.setdefault(key, __fallback(key))
-
- rule = rules.setdefault(key, rule)
- if key not in distances:
- if inheritedDistances is not None and inherit(key) == rule:
- distances.setdefault(key, inheritedDistances[key] + 1)
- else:
- distances[key] = 0
-
- except:
- key not in distances
-
- if computing:
- stack = get_stack(get_ident())
- if stack:
- stack[-1] = min(stack[-1], distances[key])
-
-
- return rule
-
-
- def setRule(key, rule):
- buffer[key] = rule
- old = rules.get(key, rule)
- if old is not rule and old != rule:
- raise InputConflict(key, old, rule)
-
-
-
- def getValue(key):
-
- try:
- value = values[key]
- except KeyError:
- this_thread = get_ident()
- rule = getRule(key)
- stack = set_stack(this_thread, [])
- stack.append(distances[key])
-
- try:
- value = key.__apply__(key, rule)
- finally:
- distance = stack.pop()
- if not stack:
- del computing[this_thread]
- else:
- stack[-1] = min(stack[-1], distance)
-
- value = publish(distance, key, value)
-
- if computing:
- stack = get_stack(get_ident())
- if stack:
- stack[-1] = min(stack[-1], distances[key])
-
-
- return value
-
-
- def publish(distance, key, value):
- distances[key] = distance
- if distance and propagate:
- value = propagate(distance - 1, key, value)
-
- return values.setdefault(key, value)
-
-
- def child():
- s = new_state(getRule, distances, publish)
- s.parent = this
- return s
-
-
- def __fallback(key):
- old = _swap(disabled)
-
- try:
- return key.__fallback__(inherit, key)
- finally:
- _swap(old)
-
-
-
- def swap():
- if exited:
- raise ScopeError("Can't switch to an exited state")
-
- (state, get, lookup, old_child) = old = _swap(enabled)
- if lookup is disallow:
- _swap(old)
- raise DynamicRuleError('default rule or exit function tried to change states')
-
- return state
-
-
- def __enter__():
- if my_parent or exited:
- raise ScopeError("Can't re-enter a previously-entered state")
- elif active_child:
- raise ScopeError('State already has an active child')
- elif get() is this:
- raise ScopeError('State is already current')
-
- (parent, xx, xx, parents_child) = old = states[get_ident()]
- if parents_child:
- raise ScopeError('Current state already has an active child')
-
- swap()
- my_parent.append(old)
- parents_child.append(this)
- return this
-
-
- def __exit__(typ, val, tb):
- if exited:
- raise ScopeError('State already exited')
- elif not my_parent:
- raise ScopeError("State hasn't been entered yet")
- elif active_child:
- raise ScopeError("Nested state(s) haven't exited yet")
- elif get() is not this:
- raise ScopeError("Can't exit a non-current state")
-
- parents_child = my_parent[0][-1]
- _swap(my_parent.pop())
- parents_child.pop()
- exited.append(1)
- values.clear()
- return call_exitfuncs(typ, val, tb)
-
- active_child = []
- my_parent = []
- exited = []
- exit_functions = []
-
- def call_exitfuncs(typ, val, tb):
- old = _swap(disabled)
-
- try:
- for func in exit_functions:
-
- try:
- func(typ, val, tb)
- continue
- (typ, val, tb) = sys.exc_info()
- continue
-
- finally:
- _swap(old)
- del typ
- del val
- del tb
-
-
-
- def on_exit(callback):
- if exited:
- raise ScopeError('State already exited')
- elif not my_parent:
- raise ScopeError("State hasn't been entered yet")
-
- if callback not in exit_functions:
- exit_functions.append(callback)
-
-
- this = State(getRule = getRule, setRule = setRule, swap = swap, child = child, __enter__ = __enter__, __exit__ = __exit__, on_exit = on_exit)
- enabled = (this, getRule, getValue, active_child)
- disabled = (None, getRule, disallow, active_child)
- return this
-
- State.get = staticmethod(get)
- State.root = root = new_state()
- root.child = empty
- root_getrule = root.getRule
- del root.swap
- del root.__enter__
- del root.__exit__
- return (lookup, empty)
-
- (lookup, empty) = _let_there_be_state()
- del _let_there_be_state
-
- class _GeneratorContextManager(object):
-
- def __init__(self, gen):
- self.gen = gen
-
-
- def __enter__(self):
- for value in self.gen:
- return value
- else:
- raise RuntimeError("generator didn't yield")
-
-
- def __exit__(self, typ, value, traceback):
- if typ is None:
- for value in self.gen:
- raise RuntimeError("generator didn't stop")
-
- else:
-
- try:
- old = _swap_exc_info((typ, value, traceback))
-
- try:
- self.gen.next()
- finally:
- _swap_exc_info(old)
-
- raise RuntimeError("generator didn't stop after throw()")
- except StopIteration:
- exc = None
- return exc is not value
- except:
- if sys.exc_info()[1] is not value:
- raise
-
-
-
-
- manager = contextlib.contextmanager
-
- def with_(ctx, func):
- inp = ctx.__enter__()
-
- try:
- retval = func(inp)
- except:
- if not ctx.__exit__(*sys.exc_info()):
- raise
-
-
- ctx.__exit__(None, None, None)
- return retval
-
-
- def reraise():
- (typ, val, tb) = _exc_info.get(get_ident(), nones)
- if typ:
-
- try:
- raise typ, val, tb
- finally:
- del typ
- del val
- del tb
-
-
-
-
- def call_with(ctxmgr):
- return with_.__get__(ctxmgr, type(ctxmgr))
-
-
- noop = lambda key, rule: rule
-
- mngr = lambda key, rule: Action.manage(rule())
-
- call = lambda key, rule: rule()
-
- class Service(_ClassDelegate):
- __slots__ = ()
-
- def __default__(cls):
- return cls()
-
- __default__ = classmethod(__default__)
-
- def __fallback__(cls, inherit, key):
- if inherit:
- return inherit(key)
-
- return cls.__default__
-
- __fallback__ = classmethod(__fallback__)
- __apply__ = staticmethod(call)
-
- def new(cls, factory = None):
- if not factory:
- pass
- factory = cls
- state = State.child().__enter__()
-
- try:
-
- state[cls.get.im_self] = lambda : factory()
- yield cls.get()
- (None,)
- reraise()
- except:
- state.__exit__(*sys.exc_info())
- raise
-
- state.__exit__(None, None, None)
-
- new = classmethod(manager(new))
-
-
- class Scope(Service):
-
- def __init__(self):
- self.state = State.get()
- self.cache = { }
-
-
- def __compute__(cls, key, func, input):
- self = cls.get()
- cache = self.cache
- if cache is None:
- raise ScopeError(self.__class__.__name__ + ' is already exited')
- elif input is not self.state[key]:
- raise ScopeError('Redefined rule in sub-state')
- elif key in cache:
- return cache[key]
- else:
- old = self.state.swap()
-
- try:
- cache[key] = value = self.manage(func(input))
- self.state.on_exit(self.atexit)
- return value
- finally:
- old.swap()
-
-
- __compute__ = classmethod(__compute__)
-
- def manage(self, ob):
- return ob
-
-
- def atexit(self, *exc):
- self.cache = None
-
-
- def __default__(cls):
- raise RuntimeError('No %s is currently active' % cls.__name__)
-
- __default__ = classmethod(__default__)
-
- def resource(cls, func):
- return setting(func, wrap = cls.__compute__)
-
- resource = classmethod(resource)
-
- def resource_registry(cls, func):
- return registry(func, wrap = cls.__compute__)
-
- resource_registry = classmethod(resource_registry)
-
-
- class Action(Scope):
-
- def __init__(self):
- self.managers = []
- super(Action, self).__init__()
-
-
- def atexit(self, *exc):
- super(Action, self).atexit(*exc)
- managers = self.managers
- self.managers = None
- while managers:
- managers.pop().__exit__(*exc)
-
-
- def manage(self, ob):
- enter = getattr(ob, '__enter__', None)
- if enter is None:
- return ob
-
- ctx = ob
- ob = ctx.__enter__()
- self.managers.append(ctx)
- return ob
-
-
- resource = Action.resource
- resource_registry = Action.resource_registry
-
- def nop():
- pass
-
-
- class setting(object):
- __class__ = type(nop)
- func_code = nop.func_code
- func_defaults = ()
- __argnames__ = (('value', 'expr'),)
-
- def __init__(self, func, wrap = None):
- if func.func_code.co_argcount != len(self.__argnames__):
- raise TypeError(type(self).__name__ + ' function must have exactly %d argument(s)' % len(self.__argnames__))
-
- for var, names in enumerate(zip(func.func_code.co_varnames, self.__argnames__)):
- if var not in names:
- raise TypeError(type(self).__name__ + " function argument %d must be named '%s'" % (num + 1, "' or '".join(names)))
- continue
-
- if self.__argnames__:
- if not (func.func_defaults) or len(func.func_defaults) != 1:
- raise TypeError(type(self).__name__ + ' function must have a default value for last argument')
-
- self.__function__ = func
- self.__module__ = func.__module__
- self.__name__ = func.__name__
- self.__doc__ = func.__doc__
- if wrap:
- self.__wrap__ = wrap
-
-
- __call__ = lookup
-
- def __apply__(self, key, input):
- return self.__wrap__(key, self.__function__, input)
-
-
- def __wrap__(self, key, func, input):
- return func(input)
-
-
- def __fallback__(self, inherit, key):
- if inherit is None:
- return self.__function__.func_defaults[0]
- else:
- return inherit(key)
-
-
- def __ilshift__(self, value):
- State[self] = value
- return self
-
-
- def __repr__(self):
- if self.__module__:
- return self.__module__ + '.' + self.__name__
-
- return self.__name__
-
-
- def __mod__(self, other):
- if self.__function__.func_code.co_varnames[len(type(self).__argnames__) - 1] == 'expr':
- return 'lambda: ' + other
-
- return other
-
-
-
- def _with_prefix(.0, suffix):
- (pre, func) = .0
- return func(pre + suffix)
-
-
- def _prefixer(prefix, func):
- return _with_prefix.__get__((prefix, func), tuple)
-
-
- class wildcard(setting):
-
- def __init__(self, registry):
- self.__module__ = registry.__module__
- self.__name__ = registry.__name__ + '.*'
- self.__function__ = registry.__function__
- self.__namespace__ = registry
-
-
- def __mod__(self, other):
- return 'lambda suffix: ' + other
-
-
- def __apply__(self, key, input):
- return input
-
-
- def __fallback__(self, inherit, key):
- parent = self.__namespace__
- if parent.__namespace__:
- func = State.get(parent.__namespace__['*'])
- if func is not None:
- prefix = self.__name__.split('.')[-2] + '.'
- return _prefixer(prefix, func)
-
-
-
-
-
- class registry(setting):
- __argnames__ = (('suffix',), ('value', 'expr'))
- func_code = (lambda key, default: pass).func_code
- func_defaults = (None,)
-
- def __init__(self, func, ns = None, name = '', wrap = None):
- setting.__init__(self, func, wrap)
- if not name:
- pass
- self.__name__ = self.__name__
- self.__namespace__ = ns
- self.__contents__ = {
- '*': wildcard(self) }
-
-
- def __getitem__(self, key):
- if '.' in key:
- for key in key.split('.'):
- self = self[key]
-
- return self
-
-
- try:
- return self.__contents__[key]
- except KeyError:
- s = self.__dict__[key] = self.__contents__[key] = registry(self.__function__, self, self.__name__ + '.' + key, self.__dict__.get('__wrap__'))
- return s
-
-
-
- def __getattr__(self, key):
- if key.startswith('__') and key.endswith('__'):
- raise AttributeError(key)
-
- return self[key]
-
-
- def __contains__(self, key):
- if '.' in key:
- for key in key.split('.'):
- if key not in self.__contents__:
- return False
-
- self = self[key]
- else:
- return True
-
- return key in self.__contents__
-
-
- def __iter__(self):
- return [](_[1])
-
-
- def __call__(self, key, default = None):
- if key not in self:
- return default
-
- return lookup(self[key])
-
-
- def __apply__(self, key, input):
- return self.__wrap__(key, self.__function__.__get__(''), input)
-
-
- def __fallback__(self, inherit, key):
- if self.__namespace__:
- suffix = key.__name__[len(self.__namespace__.__name__) + 1:]
- finder = State.get(self.__namespace__['*'])
- if finder is not None:
- return finder(suffix)
-
-
- if inherit is not None:
- return inherit(key)
-
- while self.__namespace__:
- self = self.__namespace__
- suffix = key.__name__[len(self.__name__) + 1:]
- return self.__function__(suffix)
-
-
- def __setitem__(self, key, value):
- if value is not self[key]:
- raise TypeError('Registries are read-only')
-
-
-
- def __setattr__(self, key, value):
- if key.startswith('__') and key.endswith('__'):
- return object.__setattr__(self, key, value)
- else:
- self[key] = value
-
-
-
- class Source(object):
- __slots__ = ('filename', '__weakref__')
-
- def __init__(self, filename, source = None):
- global linecache
- import linecache
- self.filename = filename
- if source is not None:
- cache_source(filename, source, self)
-
-
-
- def compile(self, *args, **kw):
- return Line(''.join(self), self, 1).compile(*args, **kw)
-
-
- def __getitem__(self, key):
- return Line(linecache.getlines(self.filename)[key], self, key + 1)
-
-
- def __repr__(self):
- return 'Source(%r)' % self.filename
-
-
- def recode(self, code, offset = 0):
- import new
- if not isinstance(code, new.code):
- return code
-
- return code.co_nlocals(code.co_stacksize, code.co_flags, code.co_code, tuple, [], []([ self.recode(c, offset) for c in code.co_consts ] + [
- self]), code.co_names, code.co_varnames, code.co_filename, code.co_name, code.co_firstlineno + offset, code.co_lnotab, code.co_freevars, code.co_cellvars)
-
-
-
- class Line(str):
-
- def __new__(cls, text, source, line):
- return str.__new__(cls, text)
-
-
- def __init__(self, text, source, line):
- self.source = source
- self.line = line
-
-
- def compile(self, *args, **kw):
- code = compile(self, self.source.filename, *args, **kw)
- return self.source.recode(code, self.line - 1)
-
-
- def eval(self, *args):
- return eval(self.compile('eval'), *args)
-
-
- def splitlines(self, *args, **kw):
- return [ Line(line, self.source, self.line + offset) for offset, line in enumerate(str.splitlines(self, *args, **kw)) ]
-
- for m in [
- 'capitalize',
- 'center',
- 'expandtabs',
- 'ljust',
- 'lower',
- 'lstrip',
- 'replace',
- 'rjust',
- 'rstrip',
- 'strip',
- 'swapcase',
- 'title',
- 'translate',
- 'upper',
- 'zfill',
- '__add__',
- '__radd__',
- '__getslice__',
- '__mod__',
- '__rmod__']:
- if hasattr(str, m):
- locals()[m] = (lambda f: (lambda self: Line(f(self, *args, **kw), self.source, self.line))
- )(getattr(str, m))
- continue
-
-
-
- def replaces(target):
-
- def decorator(cls):
- if not issubclass(cls, Service):
- raise TypeError('context.replaces() can only be used in a context.Service subclass')
-
- cls.get = staticmethod(target.get)
- return cls
-
- decorate_class = decorate_class
- import peak.util.decorators
- decorate_class(decorator)
- cdict = sys._getframe(1).f_locals
- if cdict.setdefault('get', target.get) is not target.get:
- raise ValueError('replaces() must be used only once per class; there is already a value for ``get``: %r' % (cdict['get'],))
-
-
-